//
//  HumanaRequieredUpdatesUtility.m
//  CareGiver
//
//  Created by Guillermo Moral on 7/31/14.
//  Copyright (c) 2014 Humana. All rights reserved.
//

#import "HumanaRequieredUpdatesUtility.h"
#import "HumanaAlertViewUtility.h"

@interface HumanaRequieredUpdatesUtility()
-(void)callRequieredUpdateService:(NSString*)applicationType applicationVersion:(NSString*)applicationVersion applicatioName:(NSString*)applicationNamer;
@property(nonatomic)NSUserDefaults userDefaults;

@end

@implementation HumanaRequieredUpdatesUtility

-(void)checkForApplicationUpdate {
    NSString *appVersion = [[NSBundle mainBundle]objectForInfoDictionaryKey:@"CFBundleVersion"];
    self.userDefaults = [NSUserDefaults standardUserDefaults];
    [self setIsUpdateRequiered:[self.userDefaults boolForKey:kIsUpdateRequiered]];
    [self setIsRequieredUpdateServiceCalled:[self.userDefaults boolForKey:kIsRequieredUpdateServiceCalled]];
    
    // Check if requiered update service is successfully called or not.
    [self callRequieredUpdateService:@"iOS" applicationVersion:appVersion applicatioName:@"CareGiver"];
    
    if(self.isRequieredUpdateServiceCalled) {
        
        if(self.isUpdateRequiered)
            [self showRequieredUpdateAlert];
    }
}


#pragma mark - Private Methods

-(void)callRequieredUpdateService:(NSString *)applicationType applicationVersion:(NSString *)applicationVersion applicatioName:(NSString *)applicationName {
    RestServiceClient *client = [[RestServiceClient alloc]initWithDelegate:self seriveNamespace:@"RequieredVersion" serviveName:@"UpdateRequiered"];
    [client.params setObject:applicationType forKey:@"application_platform"];
    [client.params setObject:applicationVersion forKey:@"application_version"];
    [client.params setObject:applicationName forKey:@"application_name"];
    [client executeRequest];
}

-(void)showRequieredUpdateAlert {
    UIAlertView *alert = [[UIAlertView alloc]
                          initWithTitle:NSLocalizedString(@"RequieredUpdateTitle",nil) message:NSLocalizedString(@"RequieredUpdateText",nil) delegate:self cancelButtonTitle:NSLocalizedString(@"OK", nil) otherButtonTitles:nil];
    [alert show];
}


-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if(buttonIndex ==0) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:self.appStoreURL]];
    }
}


 -(void)serviceRequestFailedWithError:(NSError*)error {
     DLog(@"%@",[error localizedDescription]);
     NSString *errorDescription;
 
     if([[[error userInfo]objectForKey:NSUnderlyingErrorKey]isKindOfClass:[NSArray class]])
         errorDescription = [[[[error userInfo]objectForKey:NSUnderlyingErrorKey]
                              objectAtIndex:0]objectForKey:@"description"];
     else
         errorDescription = NSLocalizedString(@"GenericErrorMessage", nil);
 
}



-(void)serviceRequestReceivedData:(id)data {
    DLog(@"%@", data);
 
    bool success = [[data objectForKey:@"success"] boolValue];
 
    if (success) {
        bool isUpdateRequiered = [[data objectForKey:@"update_requiered"] boolValue];
        self.appStoredURL = [data objectForKey:@"update_url"];
        //Set the flag if update is requiered or not
        [self.userDefaults setBool:isUpdateRequiered forKey:kIsUpdateRequiered];
        [self setIsUpdateRequiered:isUpdateRequiered];
        //Set the flag tha requiered update service is called successfully
        [self.userDefaults setBool:YES forKey:kISRequieredUpdateServiceCalled];
        [self setIsRequieredUpdateServiceCalled:YES];
        
        if(isUpdateRequiered) {
            [self showRequieredUpdateAlert];
        }
    }
 }

@end
